package cn.edu.cug.cs.gtl.docsrv.auth.dao.imp;

import cn.edu.cug.cs.gtl.docsrv.auth.dao.RoleDao;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthRole;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthRoleMapper;
import org.springframework.stereotype.Component;

@Component
public class RoleDaoImpl implements RoleDao {
    @Override
    public boolean insertRole(AuthRole authRole) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthRoleMapper mapper = s.getMapper(AuthRoleMapper.class);
            mapper.insert(authRole);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateRole(AuthRole authRole) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthRoleMapper mapper = s.getMapper(AuthRoleMapper.class);
            mapper.updateByRoleName(authRole);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteRole(String authRoleName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthRoleMapper mapper = s.getMapper(AuthRoleMapper.class);
            mapper.deleteByRoleName(authRoleName);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public AuthRole getRole(String authRoleName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthRoleMapper mapper = s.getMapper(AuthRoleMapper.class);
            AuthRole authRole = mapper.selectByRoleName(authRoleName);
            s.close();
            return authRole;
        }
        return null;
    }
}
